package xyz.gzdxc.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.gzdxc.common.mysql.entity.SessionEntity;
import xyz.gzdxc.common.mysql.entity.UserTokenEntity;
import xyz.gzdxc.common.mysql.repository.SessionRepository;
import xyz.gzdxc.common.mysql.repository.UserTokenRepository;
import xyz.gzdxc.common.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SessionRepository sessionRepository;

    @Autowired
    UserTokenRepository userTokenRepository;

    @Override
    public UserTokenEntity getUserToken(String loginSession) {
        if(loginSession == null){
            return null;
        }
        // first get user id by loginSession, then find the user data by user id
        SessionEntity sessionEntity = sessionRepository.findByLoginSession(loginSession);
        if(sessionEntity != null && sessionEntity.getUserId() != null){
            Long userId = sessionEntity.getUserId();
            UserTokenEntity userTokenEntity = userTokenRepository.findById(userId).get();
            return userTokenEntity;
        }
        return null;
    }
}
